import BrowserService from "../../../browser.service";
import { LibrariesItemConfig } from "../../../config";
import { consoleLog } from "../../../utils";
import LibrariesCasesController from "./add-cases";
import LibrariesPlansController from "./add-plans";
import LibrariesReviewsController from "./add-reviews";

const {pageUrl, name} = LibrariesItemConfig;
export default class TesthubLibrariesItemController extends BrowserService{
  constructor() {
    super();
  }
  async init() {
    await this.loadingPagNav()
    await this.registerTest()
  }
  async loadingPagNav() {
    // 比对当前页面的url如果不是项目页面访问项目页面
    const nowPageUrl = this.page.url();
    if (nowPageUrl !== pageUrl) {
      await this.page.goto(pageUrl, { waitUntil: 'networkidle' });
    }
    // 因为标识是唯一的不能重复的[比对项目名与标识需要同时存在]
    const librariesNameVisible = await this.page.isVisible(
      `tbody tr td span:has-text("${name}")`
    );
    if (librariesNameVisible) {
      // 点击项目列表跳转到需求页面
      await this.page.click(`tbody tr td span:has-text("${name}")`);
    } else {
      await consoleLog(`Testhub找不到测试库`, this.logPath);
    }
  }
  async registerTest() {
    // await new LibrariesCasesController().init();
    await new LibrariesReviewsController().init();
    await new LibrariesPlansController().init();
  }
}